package com.club.chelseaclub;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabWidget;
import android.widget.TextView;

import com.club.chelseaclub.tab.Tab1Activity;
import com.club.chelseaclub.tab.Tab2Activity;
import com.club.chelseaclub.tab.Tab3Activity;
import com.club.chelseaclub.tab.Tab4Activity;
import com.club.chelseaclub.tab.Tab5Activity;
import com.club.chelseaclub.tab.TabItem;

/**主程序入口*/
public class MainActivity extends TabHostActivity {

	List<TabItem> mItems;
	private LayoutInflater mLayoutInflater;
	
	/**在初始化TabWidget前调用
	 * 和TabWidget有关的必须在这里初始化*/
	@Override
	protected void prepare() {
		TabItem home = new TabItem(
				"首页",									// title
				R.drawable.menu_home,					// icon
				R.drawable.example_tab_item_bg,			// background
				new Intent(this, Tab1Activity.class));	// intent
		
		TabItem info = new TabItem(
				"新闻",
				R.drawable.menu_news,
				R.drawable.example_tab_item_bg,
				new Intent(this, Tab2Activity.class));
		
		TabItem msg = new TabItem(
				"排行",
				R.drawable.menu_range,
				R.drawable.example_tab_item_bg,
				new Intent(this, Tab3Activity.class));
		
		TabItem square = new TabItem(
				"信息",
				R.drawable.menu_info,
				R.drawable.example_tab_item_bg,
				new Intent(this, Tab4Activity.class));
		
		TabItem more = new TabItem(
				"讨论",
				R.drawable.icon_more,
				R.drawable.example_tab_item_bg,
				new Intent(this, Tab5Activity.class));
		
		mItems = new ArrayList<TabItem>();
		mItems.add(home);
		mItems.add(info);
		mItems.add(msg);
		mItems.add(square);
		mItems.add(more);

		// 设置分割线
		TabWidget tabWidget = getTabWidget();
		tabWidget.setDividerDrawable(R.drawable.tab_divider);
		
		mLayoutInflater = getLayoutInflater();
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setCurrentTab(0);
	}
	
	/**tab的title，icon，边距设定等等*/
	@Override
	protected void setTabItemTextView(TextView textView, int position) {
		textView.setPadding(3, 3, 3, 3);
		textView.setText(mItems.get(position).getTitle());
		textView.setBackgroundResource(mItems.get(position).getBg());
		textView.setCompoundDrawablesWithIntrinsicBounds(0, mItems.get(position).getIcon(), 0, 0);
		
	}
	
	/**tab唯一的id*/
	@Override
	protected String getTabItemId(int position) {
		return mItems.get(position).getTitle();	// 我们使用title来作为id，你也可以自定
	}

	/**点击tab时触发的事件*/
	@Override
	protected Intent getTabItemIntent(int position) {
		return mItems.get(position).getIntent();
	}

	@Override
	protected int getTabItemCount() {
		return mItems.size();
	}
	
	/**自定义头部文件*/
	@Override
	protected View getTop() {
		return mLayoutInflater.inflate(R.layout.example_top, null);
	}

}
